<?php
namespace app\index\Controller;
use app\index\model\Usermodel;
use think\captcha\Captcha;
use think\Controller;
use think\Session;
use think\Loader;
use think\Request;
use think\Cookie;

class User extends Controller{


    /*注册发送手机验证码*/
    public function getcode(){
        $rand = rand(1000,9999);
        Session::set('rand',$rand);
        $request = Request::instance();
        $call = $request->param('callback');
        $userphone = $request->get('userphone');
        $model = new Usermodel();
        $row = $model->chick($userphone);
        if($row){
            echo $call."(0)";
            return;
        }else{
            $res = $this->sendTemplateSMS($userphone,array($rand,'3'),"1");

            if($res){
                echo $call."(1)";
                return;
            }else{
                echo $call."(2)";
                return;
            }
        }
    }

    /*登陆发送手机验证码*/
    public function code(){
        $rand = rand(1000,9999);
        Session::set('rand',$rand);
        $request = Request::instance();
        $call = $request->param('callback');
        $userphone = $request->get('userphone');
        $model = new Usermodel();
        $row = $model->chick($userphone);
        if($row){
            $res = $this->sendTemplateSMS($userphone,array($rand,'3'),"1");
            if($res){
                echo $call."(1)";
                return;
            }else{
                echo $call."(2)";
                return;
            }
        }else{
            echo $call."(3)";
        }
    }
    /*发送手机验证码的类  由于云通讯测试账号不同意 请注意修改云通讯的资料 */
    public  function sendTemplateSMS($to,$datas,$tempId){
        $accountSid= '8a216da85f341b69015f38feb9d70311';
        $accountToken= 'f031483b01fe4f0d8c9e33f5cace4c66';
        $appId='8a216da85f341b69015f38febb5a0318';
        $serverIP='app.cloopen.com';
        $serverPort='8883';
        $softVersion='2013-12-26';
        Loader::import("REST.php");
        $rest = new \REST($serverIP,$serverPort,$softVersion);
        $rest->setAccount($accountSid,$accountToken);
        $rest->setAppId($appId);
        $result = $rest->sendTemplateSMS($to,$datas,$tempId);
        if($result == NULL ) {
            return  false;

        }
        if($result->statusCode!=0) {
            return false;

        }else{
            return true;
        }
    }


    /*个人用户注册*/
    public function index(){
        $request = Request::instance();
        $call = $request->param('callback');
        $userphone = $request->get('userphone');

        $userpwd = md5($request->get('userpwd'));
        $smsCode = $request->get('smsCode');
        $rand = Session::get('rand');

        if($smsCode!=$rand){
            echo $call."(0)";
        }else{
            $model = new Usermodel();
            $data=array(
                'userphone'=>$userphone,
                'userpwd'=>$userpwd,
                'regtime'=>time()
            );
            $res = $model->regist($data);

            $_SESSION['userphone']=$res['userphone'];
            session('uid',$res['id']);
            $res=$_SESSION['userphone'];

            echo $call."(".$res.")";
        }
    }



    /*图形验证码*/
    public function verify(){
        $config =    [
            'fontSize'    =>   16,
            'length'      =>   4,
            'useNoise'    =>   true,
            'imageH'	  =>   42,
            'imageW'      =>   120,
        ];
        $captcha = new Captcha($config);
       return $captcha->entry();
    }

    /*个人密码登陆验证*/
    public function login(){
        $request = Request::instance();
        $call =  $request->get('callback');
        $userphone =  $request->get('userphone');
        $pwd = md5($request->get('userpwd'));
        $smsCode =  $request->get('smsCode');
        $captcha = new Captcha();
        $row = $captcha->check($smsCode);

        $model = new Usermodel();
        $res = $model->chick($userphone);

        if(!$res){
            echo $call."(1)";//用户名不存在
            return;
        }else if($pwd!=$res['userpwd']){
            echo $call."(2)";//密码错误
            return;
        }else if(!$row){
            echo $call."(3)";//验证码错误
            return;
        }else{

            session('uid',$res['id']);
            echo $call."(4)";
        }

    }

}